package com.module.api.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.module.admin.log.pojo.BizLog;
import com.module.admin.log.service.BizLogService;
import com.system.comm.utils.FrameStringUtil;
import com.system.comm.utils.FrameTimeUtil;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;

/**
 * service接口
 * @author yuejing
 * @date 2016年1月29日 下午9:31:59
 * @version V1.0.0
 */
@RestController
public class ApiBizLogController {

    private final Logger LOGGER = LoggerFactory.getLogger(ApiBizLogController.class);
    
	@Autowired
	private BizLogService bizLogService;

	/**
	 * 记录日志
	 */
	@RequestMapping(value = "/api/bizLog/save")
	public ResponseFrame save(String traceId, String reqParam1, String reqParam2, String reqParam3,
			String reqTime, String dtlString) {
		try {
			BizLog bizLog = new BizLog();
			bizLog.setTraceId(traceId);
			bizLog.setReqParam1(reqParam1);
			bizLog.setReqParam2(reqParam2);
			bizLog.setReqParam3(reqParam3);
			if (FrameStringUtil.isNotEmpty(reqTime)) {
				bizLog.setReqTime(FrameTimeUtil.parseDate(reqTime));
			}
			ResponseFrame frame = bizLogService.save(bizLog, dtlString);
			return frame;
		} catch (Exception e) {
			LOGGER.error("处理业务异常: " + e.getMessage(), e);
			return new ResponseFrame(ResponseCode.SERVER_ERROR);
		}
	}

	/**
	 * 查询链路
	 */
	@RequestMapping(value = "/api/bizLog/get")
	public ResponseFrame save(String traceId) {
		try {
			ResponseFrame frame = new ResponseFrame();
			BizLog data = bizLogService.getDtl(traceId);
			frame.setBody(data);
			frame.setSucc();
			return frame;
		} catch (Exception e) {
			LOGGER.error("处理业务异常: " + e.getMessage(), e);
			return new ResponseFrame(ResponseCode.SERVER_ERROR);
		}
	}

}